package cn.dgut.training.service.impl;

import cn.dgut.training.controller.GoodsService;
import cn.dgut.training.dto.user.CollectDTO;
import cn.dgut.training.entity.goods.Goods;
import cn.dgut.training.entity.user.Collect;
import cn.dgut.training.mapper.CollectMapper;
import cn.dgut.training.service.ICollectService;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.user.CollectVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService {

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private GoodsService goodsService;

    @Override
    public BaseVO<CollectVO> listCollect(CollectDTO collectDTO) {
        IPage<Collect> iPage=new Page<>();
        BeanUtils.copyProperties(collectDTO,iPage);
        List<CollectVO> collectVOList=new ArrayList<>();
        IPage<Collect> collectIPage=collectMapper.selectPage(iPage,new QueryWrapper<Collect>().lambda().eq(Collect::getUserId,collectDTO.getUserId()));
        List<Collect> collectList=collectIPage.getRecords();
        for(Collect collect:collectList){
            CollectVO collectVO=new CollectVO();
            Goods goods=goodsService.getOneGoods(collect.getValueId());
            BeanUtils.copyProperties(collect,collectVO);
            collectVO.setPicUrl(goods.getPicUrl());
            collectVO.setRetailPrice(goods.getRetailPrice());
            collectVO.setBrief(goods.getBrief());
            collectVO.setName(goods.getName());
            collectVOList.add(collectVO);
        }
        BaseVO<CollectVO> baseVO=new BaseVO<>(iPage);
        baseVO.setList(collectVOList);
        return baseVO;
    }

    @Override
    public void addOrDelete(CollectDTO collectDTO) {
        Collect collect=collectMapper.selectOne(new QueryWrapper<Collect>()
                .lambda()
                .eq(Collect::getUserId,collectDTO.getUserId())
                .eq(Collect::getCollectType,collectDTO.getCollectType())
                .eq(Collect::getValueId,collectDTO.getValueId()));

        if(collect!=null){
            collectMapper.deleteById(collect.getId());
        }
        else {
            Collect newCollect=new Collect();
            newCollect.setCollectType(collectDTO.getCollectType());
            newCollect.setUserId(collectDTO.getUserId());
            newCollect.setValueId(collectDTO.getValueId());
            collectMapper.insert(newCollect);
        }
    }
}
